/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */

/*
 * PathwaySelectionPanel.java
 *
 * Created on 27-May-2011, 16:50:38
 */
package acid.plugin.gui;

import acid.plugin.expression.pathfinding.FindPaths;
import cytoscape.Cytoscape;
import cytoscape.data.CyAttributes;
import cytoscape.task.ui.JTaskConfig;
import cytoscape.task.util.TaskManager;
import giny.model.Node;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;
import javax.swing.JOptionPane;

/**
 *
 * @author morgan
 */
public class PathwaySelectionPanel extends javax.swing.JPanel
        implements ActionListener
{

    /** Creates new form PathwaySelectionPanel */
    public PathwaySelectionPanel()
    {
        initComponents();
        String[] attributes = Cytoscape.getNodeAttributes().getAttributeNames();
        for (String s: attributes)
        {
            if (s.equals("expression_vals"))
            {
                expressionCheckBox.setEnabled(true);
                expressionCheckBox.setToolTipText("Adds another column with the "
                        + "average expression value of each pathway");
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pathStart = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        pathEnd = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        numPathsSpinner = new javax.swing.JSpinner();
        jPanel3 = new javax.swing.JPanel();
        expressionCheckBox = new javax.swing.JCheckBox();

        jLabel4.setText("jLabel4");

        setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setMinimumSize(new java.awt.Dimension(400, 130));
        setPreferredSize(new java.awt.Dimension(400, 130));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel1.setBorder(null);
        jPanel1.setAlignmentX(0.0F);
        jPanel1.setMaximumSize(new java.awt.Dimension(33035, 30));
        jPanel1.setPreferredSize(new java.awt.Dimension(500, 30));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        jLabel1.setText("Find pathways between: ");
        jLabel1.setAlignmentX(1.0F);
        jLabel1.setMaximumSize(new java.awt.Dimension(266, 18));
        jLabel1.setMinimumSize(new java.awt.Dimension(266, 18));
        jLabel1.setPreferredSize(new java.awt.Dimension(266, 18));
        jPanel1.add(jLabel1);

        pathStart.setEditable(false);
        pathStart.setMaximumSize(new java.awt.Dimension(2147483647, 28));
        jPanel1.add(pathStart);

        jLabel2.setText(" and ");
        jLabel2.setAlignmentX(1.0F);
        jPanel1.add(jLabel2);

        pathEnd.setEditable(false);
        pathEnd.setMaximumSize(new java.awt.Dimension(2147483647, 28));
        jPanel1.add(pathEnd);

        add(jPanel1);

        jPanel2.setBorder(null);
        jPanel2.setAlignmentX(0.0F);
        jPanel2.setMaximumSize(new java.awt.Dimension(33035, 30));
        jPanel2.setMinimumSize(new java.awt.Dimension(282, 30));
        jPanel2.setPreferredSize(new java.awt.Dimension(400, 30));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        jLabel5.setText("Number of potential pathways to find: ");
        jLabel5.setAlignmentX(0.5F);
        jPanel2.add(jLabel5);

        numPathsSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(100), Integer.valueOf(0), null, Integer.valueOf(1)));
        numPathsSpinner.setMaximumSize(new java.awt.Dimension(100, 28));
        jPanel2.add(numPathsSpinner);

        add(jPanel2);

        jPanel3.setBorder(null);
        jPanel3.setAlignmentX(0.0F);
        jPanel3.setMaximumSize(new java.awt.Dimension(33035, 30));
        jPanel3.setMinimumSize(new java.awt.Dimension(303, 30));
        jPanel3.setPreferredSize(new java.awt.Dimension(400, 30));
        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

        expressionCheckBox.setText("Use expression data to rank paths");
        expressionCheckBox.setToolTipText("Expression data must be loaded in order to use this feature.");
        expressionCheckBox.setEnabled(false);
        expressionCheckBox.setMaximumSize(new java.awt.Dimension(300, 30));
        expressionCheckBox.setMinimumSize(new java.awt.Dimension(163, 30));
        expressionCheckBox.setPreferredSize(new java.awt.Dimension(163, 30));
        jPanel3.add(expressionCheckBox);

        add(jPanel3);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox expressionCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSpinner numPathsSpinner;
    private javax.swing.JTextField pathEnd;
    private javax.swing.JTextField pathStart;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Set set = Cytoscape.getCurrentNetwork().getSelectedNodes();
        if (set.size() != 2)
        {
            JOptionPane.showMessageDialog(this, "Please select two nodes.");
            return;
        }

        int[] ids = new int[2];
        int count = 0;
        CyAttributes nodeAttributes = Cytoscape.getNodeAttributes();
        String start = null;
        String end = null;
        for (Object o : set)
        {
            Node node = (Node) o;
            String id = node.getIdentifier();
            ids[count++] = new Integer(id);
            if (count == 1)
            {
                start = nodeAttributes.getStringAttribute(id, ConfigurationOptions.NAME_ATT);
                pathStart.setText(start);
            }
            else if (count == 2)
            {
                end = nodeAttributes.getStringAttribute(id, ConfigurationOptions.NAME_ATT);
                pathEnd.setText(end);
            }
        }

        int i = JOptionPane.showOptionDialog(null, this, "Pathway Discovery",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
                null, null, null);
        if (i != JOptionPane.OK_OPTION)
        {
            return;
        }

        boolean useExpressionData = expressionCheckBox.isSelected();

        FindPaths task = new FindPaths(ConfigurationOptions.WEIGHT_ATT,
                ids[0], ids[1], (Integer)numPathsSpinner.getValue(), useExpressionData);

        // Configure JTask Dialog Pop-Up Box
        JTaskConfig jTaskConfig = new JTaskConfig();
        jTaskConfig.setOwner(Cytoscape.getDesktop());
        jTaskConfig.displayCloseButton(true);

        jTaskConfig.displayCancelButton(false);

        jTaskConfig.displayStatus(true);
        jTaskConfig.setAutoDispose(true);

        // Execute Task in New Thread; pops open JTask Dialog Box.
        TaskManager.executeTask(task, jTaskConfig);


        //Create a JPanel object
        PathPanel pathPanel = new PathPanel(task.getSet(), useExpressionData);

        //Add it to the control panel.
        String tabTitle = "Paths between " + start + " and " + end;
        AcidResultsPanel.getInstance().addTab(tabTitle, pathPanel);

    }
}
